package com.syqc.monitor.popup

import android.content.Context
import android.graphics.drawable.ColorDrawable
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import android.widget.PopupWindow
import androidx.core.view.isVisible
import com.syqc.monitor.databinding.PopupMapTypeBinding

/**
 *@author  hfjs
 *@time   2024/8/20 10:03
 *@desc
 */
class MapTypePopup(context: Context, private val callback: (Boolean) -> Unit) : PopupWindow() {
    private val binding = PopupMapTypeBinding.inflate(LayoutInflater.from(context))
    private val location = IntArray(2)
    private var measuredWidth = 0
    private var measuredHeight = 0

    init {
        contentView = binding.root
        isClippingEnabled = false
        initView()
        isFocusable = true //popupwindow设置焦点
        setBackgroundDrawable(ColorDrawable(0)) //设置背景
        isOutsideTouchable = true //点击外面窗口消失
        width = LinearLayout.LayoutParams.WRAP_CONTENT //设置SelectPicPopupWindow弹出窗体的宽
        height = LinearLayout.LayoutParams.WRAP_CONTENT //设置SelectPicPopupWindow弹出窗体的高

    }

    private fun initView() {
        binding.ivMapNormal.setOnClickListener {
            callback(true)
            binding.ivNormalChoose.isVisible = true
            binding.ivStarChoose.isVisible = false
        }
        binding.ivMapSate.setOnClickListener {
            callback(false)
            binding.ivNormalChoose.isVisible = false
            binding.ivStarChoose.isVisible = true
        }
    }

    fun showView(view: View, isNormal: Boolean) {
        binding.ivNormalChoose.isVisible = isNormal
        binding.ivStarChoose.isVisible = !isNormal
        if (location[0] == 0) {
            contentView.measure(
                View.MeasureSpec.UNSPECIFIED,
                View.MeasureSpec.UNSPECIFIED
            )
            measuredWidth = contentView.measuredWidth
            measuredHeight = contentView.measuredHeight
            view.getLocationOnScreen(location)
            location[0] = location[0] - measuredWidth
            location[1]

        }
        showAtLocation(view, Gravity.NO_GRAVITY, location[0], location[1])


    }
}